home *** CD-ROM | disk | FTP | other *** search
- Path: portal.gmu.edu!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: A weird thing about printf()
- Date: 10 Apr 96 10:53:11 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.829133591@rscernix>
- References: <4kflr2$5if@dewey.csun.edu>
- NNTP-Posting-Host: ues5.cern.ch
- Mime-Version: 1.0
- Content-Type: text/plain; charset=US-ASCII
- Content-Transfer-Encoding: 7bit
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <4kflr2$5if@dewey.csun.edu> kc44097@csun.edu (chen) writes:
- >
- > The program :
- >
- >#include <stdio.h>
- >
- >int answer;
- >main()
- >{
- > answer=2+2;
- > printf("The answer is %d\n");
- > return 0;
- >}
- >
- > In printf(),I lost "answer" in the end,but it works,and give me the
- >result 0.I wonder how the compiler handle this condition.
-
- It simply sends to printf whatever arguments you are asking it to pass.
- There are compilers (e.g. gcc) that can detected your mistake (if asked
- to) by analyzing the format string and comparing it to the rest of the
- argument list. There is no requirement for a compiler to perform this
- kind of testing, it's a quality of implementation issue.
-
- >Also,is this allow by C?
-
- C says that this mistake invokes "undefined behaviour" and no matter what
- the compiler does, it is the right thing, by definition.
-
- >(by this, I mean is this a leagal usage in language itself,
- >or just a mistake cause by the compiler?)
-
- It is not legal usage and it is not a mistake caused by the compiler,
- it is a mistake caused by the programmer. Yeah, I know, it's so convenient
- to blame your mistakes on the language or the compiler :-)
-
- > Can someone give me some advice ? Please e-mail me :
- > kc44097@huey.csun.edu
-
- Because your time is so much more precious than ours and you can't
- "waste" it by reading the answers in the place where you posted the
- question?
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-